from pydantic import BaseModel, Field
from typing import Optional, Any


class Result(BaseModel):
    """
    标准化的返回对象
    """

    code: int = Field(description="状态码，1表示成功，其他值表示失败")
    msg: str = Field(description="消息，描述请求的结果")
    data: Optional[Any] = Field(None, description="数据，包含实际的业务数据")

    @classmethod
    def success(cls, data=None):
        """
        封装成功的返回值
        :param data: 实际的业务数据
        :return: Result对象
        """
        return cls(code=1, msg="success", data=data)

    @classmethod
    def error(cls, msg):
        """
        封装失败的返回值
        :param msg: 错误消息
        :return: Result对象
        """
        return cls(code=0, msg=msg, data=None)
